@extends('layouts.app')
@section('title', '收货地址列表')

@section('content')
    <div class="col-lg-10 col-lg-offset-1">
        <div class="panel panel-default">
            <div class="panel-heading">
                收货地址列表
                <a href="{{ route('user_addresses.create') }}" class="pull-right">新增收货地址</a>
            </div>
            <div class="panel-body">
                <table class="table table-bordered table-striped">
                    <thead>
                    <tr>
                        <th>收货人</th>
                        <th>地址</th>
                        <th>邮编</th>
                        <th>电话</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach($addresses as $address)
                        <tr>
                            <td>{{ $address->contact_name }}</td>
                            <td>{{ $address->full_address }}</td>
                            <td>{{ $address->zip }}</td>
                            <td>{{ $address->contact_phone }}</td>
                            <td>
                                <a href="{{route('user_addresses.edit', ['address'=>$address->id])}}" class="btn btn-primary">修改</a>  {{-- ok 1 --}}
                                {{--<a href="{{route('user_addresses.edit', [$address->id])}}" class="btn btn-primary">修改</a>  --}}   {{-- ok 2 --}}

                                {{--
                                <form action="{{route('user_addresses.destroy', [$address->id])}}" method="post" style="display:inline-block">
                                    {{csrf_field()}}
                                    {{method_field('delete')}}
                                    <button class="btn btn-danger">删除</button>
                                </form>
                                --}}
                                <button class="btn btn-danger btn-del-address" data-id="{{$address->id}}">删除</button>
                            </td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>
@endsection

@push('myjs')
<script>
    $(function () {
        $('.btn-del-address').click(function(){
            var addressId=$(this).data('id')
            swal({
                title: '确定要删除吗？',
                buttons:['取消', '确定'],
                icon: 'warning',
                dangerMode:true
            }).then((yes)=>{
                if(!yes)
                    return;
                axios.delete('/user_addresses/'+addressId).then(location.reload())
            })
        })
    })
</script>
@endpush